package com.supervision.manage

import com.fly.base.util.PasswordHash
import com.supervision.domain.enums.AdminEnum
import com.supervision.domain.model.Admin
import org.slf4j.LoggerFactory
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.stereotype.Component

/**
 * @Author: carl
 * @Date: 2020-05-03 19:46
 */
@Component
class ManageBootApplicationRunner : ApplicationRunner {
    private val log = LoggerFactory.getLogger(ApplicationRunner::class.java)
    override fun run(args: ApplicationArguments?) {
        val admin = Admin.findById(1L)
        if (admin == null) {
            log.info("================ 初始化数据库数据开始 ================")
            val superAdmin = Admin()
            superAdmin.type = AdminEnum.TYPE_SYSTEM.code
            superAdmin.username = "admin"
            superAdmin.password = PasswordHash.createHash("123456")
            superAdmin.name = "admin"
            superAdmin.save()
            log.info("================ 初始化数据库数据结束 ================")
        } else {
            log.info("================ 已经初始化过数据库 ================")
        }
    }
}